﻿using System;
using System.IO;
using System.Web.Hosting;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IisHost.UI
{
    public partial class LogControl : UserControl
    {
        public string Log
        {
            get
            {
                return ViewState["Log"] as String;
            }
            set
            {
                ViewState["Log"] = value;
            }
        }

        private string FilePath
        {
            get
            {
                return Path.Combine(HostingEnvironment.MapPath("~/App_Data"), Log);
            }
        }

        public void BindData()
        {
            string text = File.Exists(FilePath)
                ? File.ReadAllText(FilePath)
                : String.Empty;
            txtLog.Text = String.IsNullOrWhiteSpace(text) 
                ? "No log records to show"
                : text;
        }

        protected void OnCommand(object sender, CommandEventArgs e)
        {
            switch(e.CommandName)
            {
                case "Clear":
                    {
                        if(File.Exists(FilePath))
                            File.Delete(FilePath);
                        Response.Redirect("~/");
                    }
                    break;
            }
        }
    }
}